package com.devdroids.cursortable;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.Contacts.People;
import android.provider.Contacts.PeopleColumns;
import android.provider.Contacts.PhonesColumns;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CursorTableActivity extends Activity {

	private static final String tag = CursorTableActivity.class.getName();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Set the column names you want to get from the contacts
		String columns[] = new String[] { BaseColumns._ID, PeopleColumns.NAME,
				PhonesColumns.NUMBER, PeopleColumns.NOTES, PeopleColumns.LAST_TIME_CONTACTED };
		Cursor cursor = managedQuery(People.CONTENT_URI, columns, null, null,
				null);

		CursorAdapter cursorAdapter = new CursorAdapter(this, cursor) {
			@Override
			public View newView(Context context, Cursor cursor, ViewGroup parent) {
				final LayoutInflater inflater = LayoutInflater.from(context);
				final View view = inflater.inflate(R.layout.list_item, parent,
						false);
				setValues(view, cursor);
				return view;
			}

			@Override
			public void bindView(View view, Context context, Cursor cursor) {
				setValues(view, cursor);
			}

			/**
			 * Simple method to set the values of the two textviews
			 * 
			 * @param view
			 *            the view of the surrounding LinearLayot
			 * @param cursor
			 *            the cursor.
			 */
			private void setValues(View view, Cursor cursor) {
				TextView nameView = (TextView) view
						.findViewById(R.id.list_item_name);
				String name = cursor.getString(cursor
						.getColumnIndex(PeopleColumns.NAME));

				if (nameView != null && name != null) {
					nameView.setText(name);
				}

				TextView numberView = (TextView) view
						.findViewById(R.id.list_item_number);
				String number = cursor.getString(cursor
						.getColumnIndex(PhonesColumns.NUMBER));

				if (numberView != null && number != null) {
					numberView.setText(number);
				}
			}

		};

		ListView listView = (ListView) findViewById(R.id.people_list_view);
		listView.setAdapter(cursorAdapter);

		// Set the column names you want to get from the contacts
		String columns2[] = new String[] { BaseColumns._ID, PeopleColumns.NAME,
				PhonesColumns.NUMBER, PeopleColumns.NOTES, PeopleColumns.LAST_TIME_CONTACTED };
		Cursor cursor2 = managedQuery(People.CONTENT_URI, columns, null, null,
				null);

		CursorAdapter cursorTableAdapter = new CursorAdapter(this, cursor2) {
			@Override
			public View newView(Context context, Cursor cursor, ViewGroup parent) {
				final LayoutInflater inflater = LayoutInflater.from(context);
				final View view = inflater.inflate(R.layout.table_row, parent,
						false);
				// setValues(view, cursor);
				setValuesByTag(view, cursor);
				Log.i(tag, "New Tablerow!");
				return view;
			}

			@Override
			public void bindView(View view, Context context, Cursor cursor) {
				setValuesByTag(view, cursor);
				Log.i(tag, "BindView!");
			}

			/**
			 * Simple method to set the values of the two textviews
			 * 
			 * @param view
			 *            the view of the parent, CursorTableView
			 * @param cursor
			 *            the cursor.
			 */
			private void setValues(View view, Cursor cursor) {
				TextView nameView = (TextView) view
						.findViewById(R.id.row_item_name);
				String name = cursor.getString(cursor
						.getColumnIndex(PeopleColumns.NAME));

				if (nameView != null && name != null) {
					nameView.setText(name);
				}

				TextView numberView = (TextView) view
						.findViewById(R.id.row_item_number);
				String number = cursor.getString(cursor
						.getColumnIndex(PhonesColumns.NUMBER));

				if (numberView != null && number != null) {
					numberView.setText(number);
				}
			}

			/**
			 * Check all the columns in the cursor and map them against the
			 * row's tags
			 * 
			 * @param view
			 *            the parent
			 * @param cursor
			 *            the current cursor
			 */
			private void setValuesByTag(View view, Cursor cursor) {
				String[] columnNames = cursor.getColumnNames();
				for (String cName : columnNames) {
					TextView textView = (TextView) view.findViewWithTag(cName);
					if (textView != null) {
						textView.setText(cursor.getString(cursor
								.getColumnIndex(cName)));
					}
				}
			}
		};

		CursorTableLayout tableLayout = (CursorTableLayout) findViewById(R.id.people_list_table);
		tableLayout.setAdapter(cursorTableAdapter);
	}
}